package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorSortDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorSortSaveDTO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSeniorSortSaveVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSeniorSortVO;
import com.xbongbong.saas.service.PublicGroupSeniorSortService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 公海池列表展示分组设置排序
 * @Author: lijiangren
 * @date: 2022-04-15 14:53
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/publicGroupSeniorSort")
public class PublicGroupSeniorSortController {

    @Resource
    private PublicGroupSeniorSortService publicGroupSeniorSortService;

    /**
     * 获取公海池列表的分组列表
     * @return
     */
    @RequestMapping(value = "/get/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getList(@RequestBody @Valid PublicGroupSeniorSortDTO publicGroupSeniorSortDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(publicGroupSeniorSortDTO.getSaasMark(), publicGroupSeniorSortDTO.getBusinessType(),null , publicGroupSeniorSortDTO.getDistributorMark());
                PublicGroupSeniorSortVO publicGroupSeniorSortVO = publicGroupSeniorSortService.getListByFormIdAndCreatorId(publicGroupSeniorSortDTO);
                response = new XbbResponse<>(publicGroupSeniorSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存公海池列表的分组列表
     * @return
     */
    @RequestMapping(value = "/set/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveGroupSort(@RequestBody @Valid PublicGroupSeniorSortSaveDTO publicGroupSeniorSortSaveDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorSortSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupSeniorSortSaveVO publicGroupSeniorSortVO = publicGroupSeniorSortService.saveGroupSort(publicGroupSeniorSortSaveDTO);
                response = new XbbResponse<>(publicGroupSeniorSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
